<?php
/**
 * Created by PhpStorm.
 * User: Guojunchen
 * Date: 2018/5/2
 * Time: 16:42
 */
namespace app\common\lib;

ini_set("display_errors", "on");
use OSS\OssClient; // | OSS客户端类，用户通过OssClient的实例调用接口 |
use OSS\Core\OssException; // | OSS异常类，用户在使用的过程中，只需要注意这个异常|
require_once EXTEND_PATH . '/aliyun-oss-php-sdk-master/autoload.php';




Class Oss{

    private $accessKeyId;
    private $accessKeySecret;
    private $endpoint;

    public function __construct(){

        //$this->config = $config;
        $this->accessKeyId =  config("oss.accesskey_id");
        $this->accessKeySecret =  config("oss.accesskey_secret");
        $this->endpoint =  config("oss.endpoint");
        $this->init();

    }


    public function init(){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            return $ossClient;
        } catch (\OssException $e) {
            print $e->getMessage();
        }

    }


    //阿里的图片上传
    public  function  ossUploadFile($fileName,$bucket,$typeName,$option=array()){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;
        try {
            //实例化对象 将配置传入
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            //执行阿里云上传
            $result = $ossClient->uploadFile($bucket, $fileName, $typeName,$option);
            /**
             * 这个只是为了展示
             * 可以删除或者保留下做后面的操作
             */
//            $arr = [
//                'url' => $result['info']['url'],
//                'fileName' => $fileName
//            ];

            return $result;

        } catch (\OssException $e) {
            return $e->getMessage();
        }


    }


    //阿里的图片上传
    public  function  putObjectFile($bucket, $object, $content, $options = NULL){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;
        try {
            //实例化对象 将配置传入
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            //执行阿里云上传
            $result = $ossClient->putObject($bucket, $object, $content,$options);
            /**
             * 这个只是为了展示
             * 可以删除或者保留下做后面的操作
             */

            return $result;

        } catch (\OssException $e) {
            return $e->getMessage();
        }


    }
    /**
     * 创建虚拟文件夹
     */
    public function CreateVirDir($bucket, $object, $options = NULL){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;
        //实例化对象 将配置传入
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

        $res = $ossClient->doesBucketExist($bucket, $object, $options);

        if(!$res){
            $ossClient->createObjectDir($bucket, $object, $options);
        }

    }


    /**
     * 指定的文件夹目录下
     */
    public function uploadFileDir($bucket, $prefix, $localDirectory, $exclude = '.|..|.svn|.git', $recursive = false, $checkMd5 = true){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;

        try {
            //实例化对象 将配置传入
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->uploadDir($bucket, $prefix, $localDirectory, $exclude, $recursive, $checkMd5);
        }
        catch (\OssException $e) {
                print $e->getMessage();
        }

    }


    /**
     * 列出所有的文件夹
     */
    public function listObjects($bucket, $options = NULL){
        $accessKeyId =  $this->accessKeyId;
        $accessKeySecret =  $this->accessKeySecret;
        $endpoint =  $this->endpoint;


            //实例化对象 将配置传入
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

        return $ossClient->listObjects($bucket, $options);

    }





}